<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>构造函数的this</h1>
    <p>构造函数中的this指向创建的实例</p>
    <script>

        let name = "windowName";   

        function Student(name) {
            this.name = name;

            this.sayHello = function() {
                console.log("普通函数",this.name);
            }

            this.sayHello2 = () => {
                console.log("箭头函数",this.name);
            }
        }

        let stu = new Student("奉先");
        stu.sayHello();
        stu.sayHello2();

        class People {
            constructor(name) {
                this.name = name;
            }

            sayHello = function() {
                console.log("普通函数",this.name);
            }

            sayHello2 = () => {
                console.log("箭头函数",this.name);
            }
        }

        let people = new People("袁浩哲");
        people.sayHello();
        people.sayHello2();

        console.log("-------------分隔符------------------")
        // 如果用people.sayHello，里面this会未定义
        let sh = stu.sayHello;
        sh();

        let sh2 = people.sayHello2;
        sh2();


    </script>
</body>
</html>